package androidx.pluginmgr.utils;

import java.lang.reflect.Field;

import android.app.Application;
import android.app.Instrumentation;

import androidx.pluginmgr.environment.InstrumentationProxy;

/**
 * @Desc: 代理设置
 * @Copyright:
 * @DateTime: 2022/10/27 16:51
 * @Author zlz
 * @Version 1.0
 */
public class ProxyUtils {
    public static void setAppProxy(Application application) {
        try {
            Class<?> activityThread = Class.forName("android.app.ActivityThread");
            Field mMainThread = application.getBaseContext().getClass().getDeclaredField("mMainThread");
            mMainThread.setAccessible(true);
            Object o = mMainThread.get(application.getBaseContext());
            Field mInstrumentation = o.getClass().getDeclaredField("mInstrumentation");
            mInstrumentation.setAccessible(true);
            Instrumentation instrumentation = (Instrumentation) mInstrumentation.get(o);
            //设置InstrumentationProxy代理类
            InstrumentationProxy proxy = new InstrumentationProxy(instrumentation);
            mInstrumentation.set(o, proxy);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
